package com.szxydb.dmp.admin.system.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.szxydb.dmp.admin.system.dao.SystemFormTempDao;
import com.szxydb.dmp.admin.system.model.SystemFormTemp;
import com.szxydb.dmp.admin.system.request.SystemFormCheckRequest;
import com.szxydb.dmp.admin.system.request.SystemFormItemCheckRequest;
import com.szxydb.dmp.admin.system.request.SystemFormTempSearchRequest;
import com.szxydb.dmp.admin.system.service.SystemFormTempService;
import com.szxydb.dmp.admin.system.vo.SystemConfigFormItemConfigRegListVo;
import com.szxydb.dmp.admin.system.vo.SystemConfigFormItemVo;
import com.szxydb.dmp.admin.system.vo.SystemConfigFormVo;
import com.szxydb.dmp.common.PageParamRequest;
import com.szxydb.dmp.exception.CrmebException;
import com.szxydb.dmp.utils.ValidateFormUtil;
import net.sf.jsqlparser.expression.operators.relational.RegExpMatchOperatorType;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service
public class SystemFormTempServiceImpl extends ServiceImpl<SystemFormTempDao, SystemFormTemp> implements SystemFormTempService {

    @Resource
    private SystemFormTempDao dao;

    @Override
    public List<SystemFormTemp> getList(SystemFormTempSearchRequest request, PageParamRequest pageParamRequest) {
        PageHelper.startPage(pageParamRequest.getPage(),pageParamRequest.getLimit());

        LambdaQueryWrapper<SystemFormTemp> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if(!StringUtils.isBlank(request.getKeywords())){
            lambdaQueryWrapper.eq(SystemFormTemp::getId,request.getKeywords())
                    .or().like(SystemFormTemp::getName,request.getKeywords())
                    .or().like(SystemFormTemp::getInfo,request.getKeywords());
        }
        lambdaQueryWrapper.orderByDesc(SystemFormTemp::getId);
        return dao.selectList(lambdaQueryWrapper);
    }

    @Override
    public void checkForm(SystemFormCheckRequest systemFormCheckRequest) {
        HashMap<String,String> map = new HashMap<>();
        for (SystemFormItemCheckRequest systemFormItemCheckRequest :systemFormCheckRequest.getFields()){
            map.put(systemFormItemCheckRequest.getName(),systemFormItemCheckRequest.getValue());
        }

        SystemFormTemp formTemp = getById(systemFormCheckRequest.getId());
        SystemConfigFormVo systemConfigFormVo;
        try {
            systemConfigFormVo = JSONObject.parseObject(formTemp.getContent(),SystemConfigFormVo.class);
        }catch (Exception e){
            throw new CrmebException("模板表单【"+formTemp.getName()+"】的内容不是正确的JSON格式！");
        }

        SystemConfigFormItemVo systemConfigFormItemVo;
        for (String item : systemConfigFormVo.getFields()){
            systemConfigFormItemVo = JSONObject.parseObject(item,SystemConfigFormItemVo.class);
            String model = systemConfigFormItemVo.get__vModel__();
            if (systemConfigFormItemVo.get__config__().getRequired() && map.get(model).equals("")){
                throw  new CrmebException(systemConfigFormItemVo.get__config__().getLabel()+"不能为空！");
            }

            checkRule(systemConfigFormItemVo.get__config__().getRegList(),map.get(model),systemConfigFormItemVo.get__config__().getLabel());

        }



    }

    private void checkRule(List<SystemConfigFormItemConfigRegListVo> regList,String value,String name){
        if (regList.size()>0){
            for (SystemConfigFormItemConfigRegListVo systemConfigFormItemConfigRegListVo:regList){
                if (!ValidateFormUtil.regular(value,name,systemConfigFormItemConfigRegListVo.getPattern())){
                    throw new CrmebException(systemConfigFormItemConfigRegListVo.getMessage());
                }
            }
        }
    }
}
